package day25.edu;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

// Created: 25/09/2025, ChangJiang Ru
public class CityMapTest {
    public static void main(String[] args) {
        Map map = CityMap.model;
        Set provinces = map.keySet();
        for(Object obj : provinces)
        {
            System.out.print(obj + "\t\t");
        }

        Scanner scan = new Scanner(System.in);
        String[] cities;
        while(true)
        {
            System.out.println("\nPlease select you are located in province: ");
            String province = scan.next();

            cities = (String[]) map.get(province);

            if(cities == null || cities.length == 0)
            {
                System.out.println("You input mistake of the province, please input again: ");
            }
            else
            {
                break; // Province is existing of input of province, so skip the loop
            }

        }

        for(int i = 0 ; i < cities.length; i++)
        {
            System.out.print(cities[i] + "\t\t");
        }

        while(true)
        {
            System.out.println("\nPlease select you are located in city:");
            String city = scan.next();
            /*
            for(int i = 0; i < cities.length; i++)
            {
                if(city.equals(cities[i]))
                {
                    System.out.println("Information register successfully");
                    break;
                }
            }

             */
            if(containsCity(city, cities))
            {
                System.out.println("Information register successfully");
                break;
            }

            System.out.println("Input city name is error, please input again");
        }

        scan.close();
    }

    public static boolean containsCity(String city, String[] cities)
    {
        for(int i = 0; i < cities.length; i++)
        {
            if(city.equals(cities[i]))
                return true;
        }
        return false;
    }
}

class CityMap
{
     static Map model = new HashMap();

     static
     {
         model.put("China", new String[]{"ShangHai", "BeiJing", "SuZhou", "NanJing", "ZhuHai"});
         model.put("America", new String[]{"NewYork", "California", "Washington", "Houston", "Chicago"});
         model.put("England", new String[]{"Bristol", "London", "Liverpool", "Birmingham", "Manchester"});
         model.put("France", new String[]{"Paris", "Marseille", "Lyon", "Toulouse", "Nice", "Nantes"});
     }
}